import Vue from 'vue'
// 高级权限控制指令 - 完全模拟v-if行为
const permissionAdvanced = {
  // Vue 3
  beforeMount(el, binding, vnode) {
    initPermissionElement(el, binding, vnode)
  },
  updated(el, binding, vnode) {
    updatePermissionElement(el, binding, vnode)
  },

  // Vue 2
  bind(el, binding, vnode) {
    initPermissionElement(el, binding, vnode)
  },
  update(el, binding, vnode) {
    updatePermissionElement(el, binding, vnode)
  },
}

function initPermissionElement(el, binding, vnode) {
  // 创建占位注释节点
  const comment = document.createComment(`v-permission="${binding.value}"`)

  // 保存相关信息
  el._permissionData = {
    comment: comment,
    parent: null,
    nextSibling: null,
    isRemoved: false,
    binding: binding,
    vnode: vnode,
  }

  // 检查初始权限状态
  updatePermissionElement(el, binding, vnode)
}

function updatePermissionElement(el, binding, vnode) {
  const hasPermissionResult = checkElementPermission(binding, vnode)

  if (hasPermissionResult) {
    showPermissionElement(el)
  } else {
    hidePermissionElement(el)
  }
}

function checkElementPermission(binding, vnode) {
  const { value } = binding

  if (!value) {
    console.warn('权限指令需要传入code参数')
    return false
  }

  // 获取当前路由的权限信息
  const route = vnode.context?.$route || vnode.ctx?.$route

  if (!route || !route.meta || !route.meta.permission) {
    console.warn('路由meta中未找到权限信息')
    return false
  }

  const permissions = route.meta.permission
  const permission = permissions.find((item) => item.code === value)

  if (!permission) {
    console.warn(`未找到code为 ${value} 的权限配置`)
    return false
  }

  return permission.hasPermission
}

function hidePermissionElement(el) {
  const data = el._permissionData

  if (!data.isRemoved && el.parentNode) {
    // 保存位置信息
    data.parent = el.parentNode
    data.nextSibling = el.nextSibling

    // 插入注释节点并移除元素
    data.parent.insertBefore(data.comment, el)
    data.parent.removeChild(el)
    data.isRemoved = true
  }
}

function showPermissionElement(el) {
  const data = el._permissionData

  if (data.isRemoved && data.comment.parentNode) {
    // 重新插入元素
    data.comment.parentNode.insertBefore(el, data.comment)
    data.comment.parentNode.removeChild(data.comment)
    data.isRemoved = false
  }
}

// 注册指令
const permissionDirective = Vue.directive('permission', permissionAdvanced)

// 权限检查工具函数
function hasPermission(code, router) {
  if (!code) {
    console.warn('权限检查需要传入code参数')
    return false
  }

  if (!router || !router.currentRoute) {
    console.warn('权限检查需要传入有效的router对象')
    return false
  }

  // 获取当前路由
  const currentRoute = router.currentRoute.value || router.currentRoute // 兼容Vue 2和Vue 3

  if (!currentRoute || !currentRoute.meta || !currentRoute.meta.permission) {
    console.warn('当前路由meta中未找到权限信息')
    return false
  }

  const permissions = currentRoute.meta.permission

  // 查找对应的权限
  const permission = permissions.find((item) => item.code === code)

  if (!permission) {
    console.warn(`未找到code为 ${code} 的权限配置`)
    return false
  }

  return permission.hasPermission
}

// 获取权限详细信息
function getPermission(code, router) {
  if (!code || !router || !router.currentRoute) {
    return null
  }

  const currentRoute = router.currentRoute.value || router.currentRoute

  if (!currentRoute || !currentRoute.meta || !currentRoute.meta.permissions) {
    return null
  }

  return currentRoute.meta.permissions.find((item) => item.code === code) || null
}

// 获取所有权限
function getAllPermissions(router) {
  if (!router || !router.currentRoute) {
    return []
  }

  const currentRoute = router.currentRoute.value || router.currentRoute

  if (!currentRoute || !currentRoute.meta || !currentRoute.meta.permissions) {
    return []
  }

  return currentRoute.meta.permissions
}

// 检查多个权限（AND逻辑）
function hasAllPermissions(codes, router) {
  if (!Array.isArray(codes)) {
    return false
  }

  return codes.every((code) => hasPermission(code, router))
}

// 检查多个权限（OR逻辑）
function hasAnyPermission(codes, router) {
  if (!Array.isArray(codes)) {
    return false
  }

  return codes.some((code) => hasPermission(code, router))
}

// 导出指令
export { permissionDirective, hasPermission, getPermission, getAllPermissions, hasAllPermissions, hasAnyPermission }
